package com.example.demos.util;

import lombok.Data;

@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public static Result<Void> ok() {
        ResultEnum resOk = ResultEnum.OK;
        Result<Void> result = new Result<>();
        result.code = resOk.getCode();
        result.msg = resOk.getMessage();
        return result;
    }

    public static <T> Result<T> ok(T t) {
        ResultEnum resOk = ResultEnum.OK;
        Result<T> result = new Result<>();
        result.code = resOk.getCode();
        result.msg = resOk.getMessage();
        result.data = t;
        return result;
    }

    public static Result<Void> ok(String message) {
        ResultEnum resOk = ResultEnum.OK;
        Result<Void> result = new Result<>();
        result.code = resOk.getCode();
        result.msg = message;
        return result;
    }

    public static <T> Result<T> ok(T t, String message) {
        ResultEnum resOk = ResultEnum.OK;
        Result<T> result = new Result<>();
        result.code = resOk.getCode();
        result.msg = message;
        result.data = t;
        return result;
    }

    public static Result<Void> requestError() {
        return err(ResultEnum.BAD_REQUEST);
    }

    public static Result<Void> notLoginError() {
        return err(ResultEnum.NOT_LOGIN);
    }

    public static Result<Void> unauthorizedError() {
        return err(ResultEnum.UNAUTHORIZED);
    }

    public static Result<Void> paramError() {
        return err(ResultEnum.BAD_PARAM);
    }

    public static Result<Void> notFoundError() {
        return err(ResultEnum.NOT_FOUND);
    }

    public static Result<Void> bizErr(String errorMessage) {
        Result<Void> result = requestError();
        result.msg = errorMessage;
        return result;
    }

    public static Result<Void> internalServerError() {
        return err(ResultEnum.INTERNAL_SERVER_ERROR);
    }

    public static Result<Void> err(ResultEnum errorRes) {
        Result<Void> result = new Result<>();
        result.code = errorRes.getCode();
        result.msg = errorRes.getMessage();
        return result;
    }

    public static Result<Void> err(Integer code,String message) {
        Result<Void> result = new Result<>();
        result.code = code;
        result.msg = message;
        return result;
    }

    public static <T> Result<T> error() {
        ResultEnum resError = ResultEnum.BAD_REQUEST;
        Result<T> result = new Result<>();
        result.code = resError.getCode();
        result.msg = resError.getMessage();
        result.data = null;
        return result;
    }

    private Result() {
    }

}
